/******************************************************************************
 * 	Freeais.org
 * 	http://www.freeais.org		info@freeais.org
 * 	
 *  Copyright (c) 2007 
 *  
 * 		ynnor systems GmbH
 * 		Mundsburger Damm 45
 * 		22087 Hamburg
 * 		Germany
 * 
 * 		Alexander Lotter	lotter@ynnor.de
 * 		David Schmitz		schmitz@ynnor.de
 * 
 *	This file is part of Freeais.org.
 *
 *  Freeais.org is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Freeais.org is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 ******************************************************************************/

package freeais.queue;

import java.util.concurrent.BlockingQueue;


import freeais.ais.IAISMessage;


/**
 * This class <code>AbstractAISConsumer</code> defines a consumer, which can
 * process data via the method consume taken out from a
 * <code>BlockingQueue</code>.
 * 
 * @author David Schmitz
 * 
 */
public abstract class AbstractAISConsumer implements Runnable {
	

	/** holds a reference to the queue */
	private final BlockingQueue<IAISMessage> queue;

	/** this is the stop flag */
	private boolean stop;

	/**
	 * This the default constructor of class AISConsumer.
	 * 
	 * @param queue
	 *            reference to the queue to consume from
	 */
	public AbstractAISConsumer(BlockingQueue<IAISMessage> queue) {

		this.queue = queue;
		this.stop = false;
		
	}

	public void run() {

		while (!stop) {
			try {
				IAISMessage msg = this.queue.take();
				consume(msg);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * This is the consume method.
	 * 
	 * @param msg
	 */
	public abstract void consume(IAISMessage msg);

	/**
	 * This method terminates the AISConsumer Thread.
	 */
	public void stop() {

		stop = true;


	}
}
